// kavlivelog.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sqlite3.h"
#include <string.h>
#include <vector>
#include <windows.h>
int ProcessFile(char *filename);
int getline(FILE * pfile,char * pdata,int datalen);
typedef struct TAGstquestion
{
	char szquestion[4096];
	char szop[20][4096];
	char szanswer[4096];
}stquestion;
sqlite3 *m_ppDb;
std::vector<stquestion> g_question;
int main(int argc, char* argv[])
{
//	ProcessFile(argv[1]);
	int rsti = sqlite3_open("asdf.db",&m_ppDb);
	char szsql[4096] = {0};
		sqlite3_exec(m_ppDb,"CREATE TABLE [Paper] ("
  "[ID] integer NOT NULL, "
  "[Type] integer, "
  "[Question] varchar (255), "
  "[An1] varchar (255), "
  "[An2] varchar (255), "
 " [An3] varchar (255), "
 " [An4] varchar (255), "
 " [An5] varchar (255), "
 " [AnswerTrue] varchar(255), "
 " [ErrPaper] int);",NULL,NULL,NULL);
	sqlite3_close(m_ppDb);
}
int atoutf8(char *out ,char *in)
{
	int Length = MultiByteToWideChar(CP_ACP,0,in,-1,NULL,0); 
	wchar_t *pWideCharStr = new wchar_t[sizeof(wchar_t)*(Length+1)];
	memset(pWideCharStr,0,sizeof(wchar_t)*(Length+1));
	MultiByteToWideChar(CP_ACP,0,in,strlen(in),pWideCharStr,Length*sizeof(wchar_t));
	Length = WideCharToMultiByte(CP_UTF8,0,pWideCharStr,-1,NULL,0,NULL,NULL);
	WideCharToMultiByte(CP_UTF8,0,pWideCharStr,-1,out,Length*sizeof(char),NULL,NULL);
	delete pWideCharStr;
	return 0;
}
int getline(FILE * pfile,char * pdata,int datalen)
{
	char temp = getc(pfile);
	for(int i =0;temp != -1 && temp !='\r' && temp !='\n';i++)
	{
		pdata[i] = temp;
		temp = getc(pfile);
	}
	fpos_t pos;
	fgetpos(pfile,&pos);
	pos--;
	fsetpos(pfile,&pos);
	return 0;
}
int ProcessFile(char *pfilename)
{
	char szoutfilename[1024] = {0};
	FILE * pfile =fopen(pfilename,"rb");
//	sprintf(szoutfilename,"%s.csv",pfilename);
	fpos_t pos;
//	FILE * poutfile =fopen(szoutfilename,"wb");
	char temp = 0;
	stquestion tquestion = {0};
	char question[4096] = {0};
	char filename[4096] = {0};
	char answerd[4096] = {0};

	if (pfile)
	{
		int nanswerindex = 0;
		for (;temp!=-1;)
		{
			temp=getc(pfile);
			if (temp == '\n')
			{
				fgetpos(pfile,&pos);
				temp=getc(pfile);
				fsetpos(pfile,&pos);
				if ((temp != '\n')&& (temp!=-1))
				{
					fscanf(pfile,"%s",filename);
//					fprintf(poutfile,"%s,",filename);
					temp=getc(pfile);
					if (temp==' ')
					{
						
						memset(question,0,4096);
						getline(pfile,question,4096);
						if(filename[0]>='0' && filename[0]<='9')
						{
							if(tquestion.szquestion[0] != 0)
							{
								stquestion tquestion2 = {0};
								atoutf8(tquestion2.szquestion,tquestion.szquestion);
								printf("%s\n",tquestion.szquestion);
								atoutf8(tquestion2.szanswer,tquestion.szanswer);
								//printf("%s\n",tquestion.szanswer);
								for(int i = 0;tquestion.szop[i][0] != 0;i++)
								{
									atoutf8(tquestion2.szop[i],tquestion.szop[i]);
								printf("%s\n",tquestion.szop[i]);
								}
								printf("%s\n",tquestion.szanswer);
								g_question.push_back(tquestion);
								nanswerindex =0;
								memset(&tquestion,0,sizeof(tquestion));
							}
							for(int i = strlen(question)-1;i>0;i--)
							{
								if(question[i] != 0x20)
								{
									break;
								}
								question[i] = 0;
							}
							char endflag = question[strlen(question)-1] -1;
							int nanswerdstart = -1;
							for(int i = strlen(question)-1;i>0;i--)
							{
								if(question[i] == endflag)
								{
									nanswerdstart = i+1;
									question[i-1] = 0;
									break;
								}
							}
							sprintf(tquestion.szquestion,"%s",question);
							sscanf(&question[nanswerdstart],"%s",tquestion.szanswer);
							//sscanf(&question[nanswerdstart],"%s",answerd);
							//for(int i = 0;i<strlen(answerd);i++)
							//{
							//	if(i)
							//	{
							//		sprintf(&tquestion.szanswer[strlen(tquestion.szanswer)],",");
							//	}
							//	sprintf(&tquestion.szanswer[strlen(tquestion.szanswer)],"%d",answerd[i] - 'A'+1);
							//}
						}
						else
						{
							sprintf(tquestion.szop[nanswerindex],"%s",question);///
							nanswerindex++;
						}
					}
				}				
			}
		}
							if(tquestion.szquestion[0] != 0)
							{
								stquestion tquestion2 = {0};
								atoutf8(tquestion2.szquestion,tquestion.szquestion);
								atoutf8(tquestion2.szanswer,tquestion.szanswer);
								for(int i = 0;tquestion2.szop[i][0] != 0;i++)
								{
									atoutf8(tquestion2.szop[i],tquestion.szop[i]);
								}
								g_question.push_back(tquestion);
								nanswerindex =0;
								memset(&tquestion,0,sizeof(tquestion));
							}
		fclose(pfile);
	}
	return 0;
}

